package person.bing.fbcloud.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Synchronized;
import org.springframework.stereotype.Service;
import person.bing.fbcloud.api.entity.ForumUser;
import person.bing.fbcloud.common.core.exception.CheckedException;
import person.bing.fbcloud.common.core.util.EncryptSecondUtil;
import person.bing.fbcloud.common.core.util.EncryptUtil;
import person.bing.fbcloud.mapper.ForumUserMapper;
import person.bing.fbcloud.service.ForumUserService;

import java.time.LocalDateTime;

//import person.bing.fbcloud.common.core.util.EncryptSecondUtil;

/**
 * <p>
 * 用户
 * </p>
 *
 * @author mengfb
 * @since 2019-06-24
 */
@Service
public class ForumUserServiceImpl extends ServiceImpl<ForumUserMapper, ForumUser> implements ForumUserService {
    EncryptSecondUtil encryptSecondUtil = new EncryptSecondUtil();

    /**
     * 注册
     * @param userCode
     * @param password
     */
    @Override
    public void signUp(String userCode, String password) {
        LocalDateTime now = LocalDateTime.now();
        ForumUser forumUser = new ForumUser();
        forumUser.setUserCode(userCode);
        forumUser.setUserPwd(encryptSecondUtil.AESencode(password,EncryptSecondUtil.AES));
        forumUser.setCreateTime(now);
        forumUser.setUpdateTime(now);
        this.save(forumUser);
    }

    /**
     * 登录
     * @param userCode
     * @param password
     */
    @Override
    public void signIn(String userCode, String password) {
        ForumUser forumUserByUserCode = this.getOne(Wrappers.<ForumUser>query().lambda()
                .eq(ForumUser::getUserCode, userCode));
        if (BeanUtil.isEmpty(forumUserByUserCode)){
            throw new CheckedException("该用户不存在");
        }
        String userPwd = forumUserByUserCode.getUserPwd();
        if (encryptSecondUtil.AESencode(password,EncryptSecondUtil.AES).equals(userPwd)){

        }else {
            throw new CheckedException("密码错误");
        }
    }
}
